<?php
$isReloadable = true;
$isPopUp = true;
require_once("../../../pers/inc.config.php");
require_once(OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php');

$sJavaScript = '';
switch (reqVar('azione')) {
   case 'salva':
      $msgPag .= salva();
      break;
   case 'salvac':
      $msgPag .= salva();
      if ($msgPag=='') {
         $sJavaScript = 'chiudi();';
      }
      break;
}

$sSql = "SELECT * FROM ovf_agenda_tipi_cont ORDER BY descr";
$objDB->query($sSql,'selTI');

$sCmdL  = "";

$sCmdR  = "&nbsp;&nbsp;&nbsp";
$sCmdR .=  "<a class='cmd1' href=\"javascript:submAz('salva');\">"._t("salva")."</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp";
$sCmdR .=  "<a class='cmd1' href=\"javascript:submAz('salvac');\">"._t("salva e chiudi")."</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp";
$sCmdR .=  "<a class='cmd1' href='javascript:chiudi();'>"._t("chiudi")."</a>";
$aOpt = array("width"=>600);
echo getTestaPopUpHtml("Gestione tipi contatto",$msgPag, $aOpt);
?>
<script type="text/javascript">
function submAz(az) {
   document.frm1.azione.value=az;
   document.frm1.submit();
   if (az == 'chiudi') {
      oParFrm = window.opener.document.frm1;
      if (oParFrm) {
         oParFrm.submit();
         //alert('parent reloaded');
      }
      window.close();
   }
}
function chiudi() {
   oParFrm = window.opener.document.frm1;
   if (oParFrm) {
      oParFrm.submit();
      //alert('parent reloaded');
   }
   window.close();
}
<?=$sJavaScript;?>
</script>
<?
echo getHtmlForm();
echo getHtmlHidden('xsn');
echo getHtmlHidden('azione','...');
$sTxtX = 'txt1';
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         <? echo $sCmdL;?>
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
   <tr>
      <td class="tit1" align="center" colspan='2'>
         Tipi contatto 
      </td>
   </tr>
</table>
<?
require OVF_FS_WEB_ROOT."/app/agen/_inc/inc.palliniTipoImp.php";
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" colspan='3'><?=_t("Tipi impegno")?></td>
   </tr>
<?
   while($acTI = $objDB->fetch('selTI')) {
      $sTxtX = ($sTxtX=='txt1')?'txt2':'txt1';
      if ($acTI['pallino']=='') {
         $sPallino = "&nbsp;";
      } else {
         $sPallino = "<img src=\"".OVF_URL_ROOT."/app/_img/pallino_ti_".$acTI['pallino'].".gif\" border='0' />";
      }
?>
   <tr>
      <td class="<?php echo $sTxtX?>" colspan='3'>
         <? 
         reqDef('ti_des_'.$acTI['id_tipo_cont'],$acTI['descr']);
         reqDef('ti_pal_'.$acTI['id_tipo_cont'],$acTI['pallino']);
         echo getHtmlText('ti_des_'.$acTI['id_tipo_cont'],40);
         echo getHtmlSelect('ti_pal_'.$acTI['id_tipo_cont'],$aPal);
         ?>
      </td>
   </tr>
<?
   }
?>
   <tr>
      <td class="<?php echo $sTxtX?>" colspan='3'>
         <? 
         echo getHtmlText('new_ti_des',40);
         echo getHtmlSelect('new_ti_pal',$aPal);
         ?>
      </td>
   </tr>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left" valign='bottom'>
         <? echo $sCmdL;?>
      </td>
      <td class="cmd1" align="right" valign='bottom'>
         <? echo $sCmdR;?>
      </td>
   </tr>
</table>
<?php 
echo "</form>\n";
echo getCodaPopUpHtml();

function salva() {
	global $objSess, $objDB;
	$msg = '';
   //return print_r($_REQUEST,1);
	foreach($_REQUEST as $nX => $vX) {
	   if (substr($nX,0,7)!='ti_des_') continue;
      if (trim($vX)!='') {
         $idTcont = substr($nX,7);
         $sSql = "UPDATE ovf_agenda_tipi_cont SET descr = '".reqVarAS('ti_des_'.$idTcont)."' "
      		.", pallino = '".reqVarAS('ti_pal_'.$idTcont)."'"
      		." WHERE id_tipo_cont = ".$idTcont;
      	$objDB->exec($sSql);
      }
   }
   
	if (reqVarTrim('new_ti_des')!='') {
      $sSql = "INSERT INTO ovf_agenda_tipi_cont SET descr = '".reqVarAS('new_ti_des')."' "
         .", pallino = '".reqVarAS('new_ti_pal')."'";
      $objDB->exec($sSql);
   }
   $_REQUEST['new_ti_des'] = '';
   $_REQUEST['new_ti_pal'] = '';
   return '';
}
